package spring.cloud.tasks.api.controller;

import com.google.common.collect.Lists;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import spring.cloud.tasks.api.domain.ZooKeeperCluster;
import spring.cloud.tasks.api.service.UtilsService;
import spring.cloud.tasks.api.service.ZooKeeperService;
import spring.cloud.tasks.api.utils.Response;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author hebelala
 */
@RequestMapping("/console/utils")
public class UtilsController {

    @Resource
    private UtilsService utilsService;
    @Resource
    private ZooKeeperService zooKeeperService;

    //@ApiResponses(value = {@ApiResponse(code = 200, message = "Success/Fail", response = RequestResult.class)})
    @PostMapping(value = "/checkAndForecastCron")
    public Response checkAndForecastCron(@RequestParam String cron) throws RuntimeException {
        return Response.success(utilsService.checkAndForecastCron(cron));
    }


    //@ApiResponses(value = {@ApiResponse(code = 200, message = "Success/Fail", response = RequestResult.class)})
    @PostMapping(value = "/getZooKeeperClusterKeyList")
    public Response getZooKeeperClusterKeyList() {
        List<ZooKeeperCluster> zooKeeperServiceZooKeeperClusterList = zooKeeperService.getZooKeeperClusterList();
        List<String> list = Lists.newArrayList();
        for (ZooKeeperCluster zooKeeperCluster : zooKeeperServiceZooKeeperClusterList) {
            list.add(zooKeeperCluster.getZooKeeperClusterKey());
        }
        return Response.success(list);
    }


}
